﻿namespace FallingRocks
{
    using System;
    using System.Collections.Generic;

    public class ConsoleRenderer : IRenderer
    {
        public void RenderObjects(List<IRenderable> renderableObjects)
        {
            if (renderableObjects.Count > 1)
            {
                foreach (IRenderable renderableObject in renderableObjects)
                {
                    Console.SetCursorPosition(renderableObject.Position.X, renderableObject.Position.Y);
                    Console.ForegroundColor = this.MatchColor(renderableObject.Color);
                    Console.Write(renderableObject.Visualisation);

                    Console.SetCursorPosition(renderableObject.Position.X, renderableObject.Position.Y - 1);
                    Console.Write(new string(' ', renderableObject.Visualisation.Length));
                }
            }
            else
            {
                IRenderable renderableObject = renderableObjects[0];
                Console.SetCursorPosition(renderableObject.Position.X, renderableObject.Position.Y);
                Console.ForegroundColor = this.MatchColor(renderableObject.Color);
                Console.Write(renderableObject.Visualisation);
            }
        }

        public void RenderStatic(List<IRenderable> renderableObjects, bool isSelected = false)
        {
            foreach (IRenderable renderableObject in renderableObjects)
            {
                Console.SetCursorPosition(renderableObject.Position.X, renderableObject.Position.Y);
                Console.BackgroundColor = ConsoleColor.Black;
                if (isSelected)
                {
                    Console.BackgroundColor = ConsoleColor.Green;
                }
                Console.ForegroundColor = this.MatchColor(renderableObject.Color);
                Console.Write(renderableObject.Visualisation);
            }
        }

        private ConsoleColor MatchColor(Color color)
        {
            ConsoleColor matchColor;

            switch (color)
            {
                case Color.Cyan:
                    matchColor = ConsoleColor.Cyan;
                    break;
                case Color.Blue:
                    matchColor = ConsoleColor.Blue;
                    break;
                case Color.Green:
                    matchColor = ConsoleColor.Green;
                    break;
                case Color.Red:
                    matchColor = ConsoleColor.Red;
                    break;
                case Color.Magenta:
                    matchColor = ConsoleColor.Magenta;
                    break;
                case Color.Yellow:
                    matchColor = ConsoleColor.Yellow;
                    break;
                default:
                    matchColor = ConsoleColor.White;
                    break;
            }

            return matchColor;
        }
    }
}
